package il.ac.tau.arielgue.outxing.helpers;

import il.ac.tau.arielgue.outxing.agents.FAOOrganism;
import il.ac.tau.arielgue.outxing.agents.OutXingOrganism;
import il.ac.tau.arielgue.outxing.utils.Params;
import proevolution.agents.Organism;
import proevolution.helpers.OrganismTransformation;
import repast.simphony.random.RandomHelper;

public class FAOTransformation implements OrganismTransformation {

	@Override
	public Organism transform(Organism original) {
		FAOOrganism transformed = null;

		if (original instanceof FAOOrganism) {
			transformed = (FAOOrganism) original;
		} else if (original instanceof OutXingOrganism) {
			OutXingOrganism castOriginal = (OutXingOrganism) original;
			transformed = (FAOOrganism) FAOOrganism.create(castOriginal);
			transformed.setMale(castOriginal.isMale()); // sets the gender from
			// the random created to the original's
		} else {
			throw new ClassCastException("Can't cast organism of type "
					+ original.getClass().getName() + " to type "
					+ OutXingOrganism.class.getName());
		}
		// set homozygot or heterozygot
		if (RandomHelper.nextDouble() > Params.getHeterozygotInvasionRate()) {
			transformed.setOutXingModifier(OutXingOrganism.HOMOZYGOT_BB);
		} else {
			if (RandomHelper.nextDouble() < 0.5) {
				transformed.setOutXingModifier(OutXingOrganism.HETEROZYGOT_AB);
			} else {
				transformed.setOutXingModifier(OutXingOrganism.HETEROZYGOT_BA);
			}
		}
		return transformed;
	}

	@Override
	public String getTargetName() {
		return "Fitness Associated Outcrosser";
	}
}
